package com.shuang.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shuang.common.PageObject;
import com.shuang.pojo.DbUser;
import com.shuang.service.DbUserService;

@RequestMapping("/user")
@Controller
public class Userhanlder {

	@Resource(name="dbUserService")
	private DbUserService dbUserService;
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public PageObject<DbUser> getUserList(int page,int rows){
		return dbUserService.getUserList(page,rows);
	}
	
	@RequestMapping("/updateUserStatus")
	@ResponseBody
	public int updateUserStatus(String uIds,short status)  throws Exception{
		ObjectMapper objectMapper = new ObjectMapper();  
		 JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Long.class);  
		 List<Long> list = objectMapper.readValue(uIds, javaType);
		return dbUserService.updateUserStatusById(list,status);
	}
	@RequestMapping("/getUserListByExample")
	@ResponseBody
	public PageObject<DbUser> getUserListByExample(DbUser dbUser,int page,int rows){
		return dbUserService.getUserListByExample(dbUser,page,rows);
	}
	@RequestMapping("/checkData/{content}/{type}")
	@ResponseBody
	public Map<String, Object> checkData(@PathVariable String content,@PathVariable  String type){
		//参数校验
		if(content==null||type==null){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("status", 403);
			map.put("data", "参数错误");
			return map;
		}
		return dbUserService.checkData(content, type);
	}
	@RequestMapping(value="/register" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> registerUser(DbUser dbUser,String code,HttpSession session){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			if(!session.getAttribute("registerRand").equals(code)){
				resultMap.put("status", 403);
				resultMap.put("data", "验证码不正确");
				return resultMap;
			}
			return dbUserService.createUser(dbUser);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("status", 500);
			resultMap.put("data", "内部服务器出错");
			return resultMap;
		}
	}

	@RequestMapping(value="/back/backlogin",method=RequestMethod.POST)
	public String backlogin(String uname,String upwd){
		try {
			Map<String, Object> result = dbUserService.userLogin(uname, upwd,(short)1);
			if(result.get("status").toString().equals("200")){
				return "redirect:/back?token="+result.get("token");
			}
			return "redirect:/back/login?status="+result.get("status")+"&data="+new String( (result.get("data")+"").getBytes("utf-8") ,"iso8859-1");
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/back/login?status=500&data=内部服务器出错";
		}
	}
	
	
	@RequestMapping(value="/frontLogin",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> frontLogin(String uName,String uPwd,String code,HttpSession session){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			if(session.getAttribute("loginRand")!=null&&!session.getAttribute("loginRand").equals(code)){
				resultMap.put("status", 403);
				resultMap.put("data", "验证码不正确");
				return resultMap;
			}
			Map<String, Object> result = dbUserService.userLogin(uName, uPwd,(short)0);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("status", 500);
			resultMap.put("data", "内部服务器出错");
			return resultMap;
		}
	}
	
	@RequestMapping(value="/token/{token}")
	@ResponseBody
	public Map<String, Object> checkLoginStatusByToken(@PathVariable String token){
		try {
			return dbUserService.selectUserFromRedisByToken(token);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> resultMap=new HashMap<String, Object>();
			resultMap.put("status", 500);
			resultMap.put("data", "内部服务器出错");
			return resultMap;
		}
	}
}
